package com.chatplus.application.service.provider;

import com.chatplus.application.common.exception.BadRequestException;
import com.chatplus.application.enumeration.FileChannelEnum;
import com.chatplus.application.file.FileAutoConfiguration;
import com.chatplus.application.service.file.FileService;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * 文件服务提供者
 **/
@Service
public class FileServiceProvider {
    private final Map<FileChannelEnum, FileService> fileServiceMap = new EnumMap<>(FileChannelEnum.class);
    private final FileAutoConfiguration.FileProperties fileProperties;

    public FileServiceProvider(List<FileService> fileServiceList,
                               FileAutoConfiguration.FileProperties fileProperties) {
        for (FileService fileService : fileServiceList) {
            fileServiceMap.put(fileService.getUploadChannel(), fileService);
        }
        this.fileProperties = fileProperties;
    }

    /**
     * 获取AI渠道处理器
     *
     * @param channel 支付渠道
     * @return PayChannelService
     */
    public FileService getFileService(FileChannelEnum channel) {
        return fileServiceMap.get(channel);
    }

    public FileChannelEnum getActiveFileDriver() {
        if (fileProperties == null||fileProperties.getActive() == null) {
            throw new BadRequestException("请配置文件服务");
        }
        if ("oss".equals(fileProperties.getActive())) {
            return FileChannelEnum.OSS;
        }
        if ("local".equals(fileProperties.getActive())) {
            return FileChannelEnum.LOCAL;
        }
        return null;
    }
}
